<!DOCTYPE html>
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<!-- three.js library -->
<script src='../../vendor/three.js/build/three.js'></script>

<script src="../threex-deadreckoningcontrols.js"></script>

<body style='margin : 0px; overflow: hidden; font-family: Monospace;'>
<div style='position: absolute; width: 100%; padding: 5px; font-family:Monospace; text-align:center;'>
	threex.deadreckoningcontrols - a three.js controls to do dead reckoning
	<br/>
	The <b>Cube</b> is using dead reckoning, based on the <b>torus</b>.
	<br/>
	The <b>Cube</b> is updated at every frame. The <b>torus</b> is updated 10 time per seconds
</div><script>
	//////////////////////////////////////////////////////////////////////////////////
	//		Init
	//////////////////////////////////////////////////////////////////////////////////

	// init renderer
	var renderer	= new THREE.WebGLRenderer({
		// antialias	: true,
		alpha: true
	});
	renderer.setClearColor(new THREE.Color('lightgrey'), 0)
	renderer.setSize( window.innerWidth, window.innerHeight );


	renderer.domElement.style.position = 'absolute'
	renderer.domElement.style.top = '0px'
	renderer.domElement.style.left = '0px'
	document.body.appendChild( renderer.domElement );

	// array of functions for the rendering loop
	var onRenderFcts= [];

	// init scene and camera
	var scene = new THREE.Scene();
	var camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
	scene.add( camera );

	var ambient = new THREE.AmbientLight( 0x666666 );
	scene.add( ambient );

	var directionalLight = new THREE.DirectionalLight( 0x887766 );
	directionalLight.position.set( -1, 1, 1 ).normalize();
	scene.add( directionalLight );

	//////////////////////////////////////////////////////////////////////////////////
	//		add an object in the scene
	//////////////////////////////////////////////////////////////////////////////////

	// add a torus knot	
	var geometry	= new THREE.CubeGeometry(1,1,1);
	var material	= new THREE.MeshNormalMaterial({
		transparent : true,
		opacity: 0.5,
		side: THREE.DoubleSide
	}); 
	var predictedMesh = new THREE.Mesh( geometry, material );
	predictedMesh.position.z = -3
	scene.add( predictedMesh );
	var cubeControls = new THREEx.DeadReckoningControls(predictedMesh)
	onRenderFcts.push(function(){
		cubeControls.update()
	})
		
	var geometry	= new THREE.TorusKnotGeometry(0.3,0.1,64,16);
	var material	= new THREE.MeshNormalMaterial(); 
	var knownPositionMesh	= new THREE.Mesh( geometry, material );
	knownPositionMesh.position.z	= -3
	knownPositionMesh.position.x	= -2
	scene.add( knownPositionMesh );
	
	updateKnownPosition()
	setInterval(updateKnownPosition, 1000/5)
	function updateKnownPosition(){		
		knownPositionMesh.position.x += 0.1
		knownPositionMesh.rotateX(0.02*Math.PI*2)
		// knownPositionMesh.rotateY(0.02*Math.PI*2)
		// knownPositionMesh.rotateZ(0.03*Math.PI*2)

		cubeControls.setKnownPose(knownPositionMesh.position, knownPositionMesh.quaternion, knownPositionMesh.scale)
	}
	
	//////////////////////////////////////////////////////////////////////////////////
	//		render the whole thing on the page
	//////////////////////////////////////////////////////////////////////////////////

	// render the scene
	onRenderFcts.push(function(){
		renderer.render( scene, camera );
	})

	// run the rendering loop
	var lastTimeMsec= null
	requestAnimationFrame(function animate(nowMsec){
		// keep looping
		requestAnimationFrame( animate );
		// measure time
		lastTimeMsec	= lastTimeMsec || nowMsec-1000/60
		var deltaMsec	= Math.min(200, nowMsec - lastTimeMsec)
		lastTimeMsec	= nowMsec
		// call each update function
		onRenderFcts.forEach(function(onRenderFct){
			onRenderFct(deltaMsec/1000, nowMsec/1000)
		})
	})
</script></body>
